angular.module('myControllers', []).controller('AppCtrl', function($scope) {

}).controller('MyCtrl', function($scope) {

}).controller('AddCtrl', function($scope, Util, Image) {
	$scope.form = {
		name: "",
		url: ""
	};
	$scope.add = function() {
		Image.add($scope.form).then(function(ret) {
			Util.toast(ret.info);
		});
	}
}).controller('IndexCtrl', function($scope, $ionicSlideBoxDelegate, Image, Util) {
	$scope.sliders = [];
	$scope.getSilder = function() {
		Image.getSlider().then(function(ret) {
			if (ret.status == 1 && ret.data != null) {
				$scope.sliders = ret.data;
			} else {
				var slide = "images/slide.jpg";
				$scope.sliders[0] = slide;
			}
			$ionicSlideBoxDelegate.update();
		});
	}
	$scope.getSilder();

	$scope.catalog = [];
	$scope.getCatalog = function() {
		Image.getCatalog().then(function(ret) {
			if (ret.status == 1 && ret.data != null) {
				$scope.catalog = ret.data;
			}
		});
	}
	$scope.getCatalog();

}).controller('PageCtrl', function($scope, Image, Util, $stateParams, $ionicSlideBoxDelegate, $ionicModal) {
	$scope.tips = "Loading...";
	$scope.field = $stateParams.field;
	$scope.title = $stateParams.title;
	$scope.list = {};
	$scope.page = 0;
	$scope.loadData = function() {
		Image.get($scope.field, $scope.page).then(function(ret) {
			if (ret.data != null) {
				$scope.list[$scope.page] = ret.data;
				// 更新slider
				$ionicSlideBoxDelegate.$getByHandle("page-handle").update();
			} else {
				$scope.tips = ret.info;
			}
		});
	}
	$scope.loadData();

	$scope.slide = function($index) {
		if ($index >= $scope.page) {
			// 加载下一页
			$scope.page += 1;
			$scope.loadData();
		}
	}

	// 使用modal看大图
	$ionicModal.fromTemplateUrl('modal.html', {
		scope: $scope,
		animation: 'slide-in-up'
	}).then(function(modal) {
		$scope.modal = modal;
	});
	$scope.showList = [];
	$scope.show = function(item, index) {
		$scope.showList = item;
		$ionicSlideBoxDelegate.$getByHandle("show-handle").slide(index);
		$scope.modal.show();
	}
});